package cn.itrip.auth.service.user;

import cn.itrip.auth.service.mail.MailService;
import cn.itrip.auth.service.sms.SmsService;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.common.MD5;
import cn.itrip.common.RedisAPI;
import cn.itrip.dao.user.ItripUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private ItripUserMapper itripUserMapper;

    @Resource
    private MailService mailService;

    @Resource
    private SmsService smsService;

    @Resource
    private RedisAPI redisAPI;

    //注册
    @Override
    public void itriptxCreateUser(ItripUser user)throws Exception {
        //1.添加用户信息
        itripUserMapper.insertItripUser(user);
        //2.生成激活码
        String activationCode = MD5.getMd5(new Date().toLocaleString(),32);
        //3.发送邮件
        mailService.sendActivationMail(user.getUserCode(),activationCode);
        //4.激活码缓存
        redisAPI.set("activation:"+user.getUserCode(),5*60,activationCode);
    }

    @Override
    public boolean activate(String mail, String code) throws Exception {
        //1.验证激活码
        String key = "activation:"+mail;
        if(redisAPI.exist(key)){
            if(redisAPI.get(key).equals(code)){
                Map<String,Object> param = new HashMap();
                param.put("userCode",mail);
                List<ItripUser> users = itripUserMapper.getItripUserListByMap(param);
                if(users.size()>0){
                    ItripUser user = users.get(0);
                    //2.更新用户
                    user.setActivated(1);//激活用户
                    user.setUserType(0);//自注册用户
                    user.setFlatID(user.getId());
                    itripUserMapper.updateItripUser(user);
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public ItripUser findUserByUserCode(String userCode) throws Exception {
        Map<String,Object> param = new HashMap();
        param.put("userCode",userCode);
        List<ItripUser> users = itripUserMapper.getItripUserListByMap(param);
        if(users.size()>0) {
            return users.get(0);
        }else{
            return null;
        }
    }

    @Override
    public void itriptxCreateUserByPhone(ItripUser user) throws Exception {
        //1.创建用户
        itripUserMapper.insertItripUser(user);
        //2.生成验证码
        int randomCode = MD5.getRandomCode();
        //3.发送验证码
        smsService.send(user.getUserCode(),"1",new String[]{String.valueOf(randomCode),"5"});
        //4.验证码缓存
        redisAPI.set("activation:"+user.getUserCode(),5*60,String.valueOf(randomCode));
    }

    @Override
    public boolean validatePhone(String phoneNum, String code) throws Exception {
        //1.比对验证码
        String key = "activation:" + phoneNum;
        if (redisAPI.exist(key)) {
            if (redisAPI.get(key).equals(code)) {
                ItripUser user = this.findUserByUserCode(phoneNum);
                if (null != user) {
                    //2.更新用户激活状态
                    user.setActivated(1);//激活用户
                    user.setUserType(0);//自注册用户
                    user.setFlatID(user.getId());
                    itripUserMapper.updateItripUser(user);
                    return true;
                }
            }
        }
        return false;
    }


    @Override
    public ItripUser login(String userCode, String userPassword) throws Exception {
        ItripUser user = this.findUserByUserCode(userCode);
        if(null!=user && user.getUserPassword().equals(userPassword)){
            if(user.getActivated()!=1){
                throw new Exception("用户未激活");
            }
            return user;
        }
        return null;
    }
}
